package projects

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
	"xpms/app/models"
	"xpms/cores"
	"xpms/cores/utils"
)

type ProjectTaskRelated struct {
	Id              int64 `orm:"pk;" json:"Id,string"`
	Type            int   `json:"Type,string"`
	Taskid          int64 `json:"Taskid,string"`
	Related         int64 `json:"Related,string"`
	Directionafter  int64 `json:"Directionafter,string"`
	Directionbefore int64 `json:"Directionbefore,string"`
	Created         int64
	Creatid         int64 `json:"Creatid,string"`
}
type ProjectTaskRelatedVo struct {
	Id              string
	Type            string
	Taskid          int64 `json:"Id,string"`
	Related         string
	Directionafter  string
	Directionbefore string
	Created         int64
	Creatid         int64 `json:"Creatid,string"`
}

func (this *ProjectTaskRelated) TableName() string {
	return models.TableName("projects_task_related")
}

func init() {
	orm.RegisterModel(new(ProjectTaskRelated))
}

func GetTaskRelated(id int64) (ProjectTaskRelated, error) {
	var needs ProjectTaskRelated
	var err error
	o := orm.NewOrm()
	needs = ProjectTaskRelated{Id: id}
	err = o.Read(&needs)
	if err == orm.ErrNoRows {
		return needs, nil
	}
	return needs, err
}

func GetTaskRelatedByTaskid(id int64) (Dto []ProjectTaskRelatedVo, err error) {
	o := orm.NewOrm()
	sql := "SELECT * FROM pms_projects_task_related WHERE taskid =?"
	_, err = o.Raw(sql, id).QueryRows(&Dto)
	if err == orm.ErrNoRows {
		return Dto, nil
	}
	return Dto, err
}

func TaskRelatedPageExec(condArr map[string]interface{}) (ops []ProjectTaskRelatedVo) {
	o := orm.NewOrm()
	o.Raw(taskRelatedPageSql(condArr)).QueryRows(&ops)
	return ops
}

/**
  组装sql
*/
func taskRelatedPageSql(condArr map[string]interface{}) (string, []interface{}) {
	var sql string
	sql = `
	SELECT
	  ppt.*
	FROM pms_projects_task_related  ppt
	WHERE 1 = 1 `
	param := make([]interface{}, 0, 0)

	if !utils.IsEmpty(condArr["taskid"]) {
		sql = sql + ` and  ppt.taskid in(`
		sql1, value := cores.SqlInParam(condArr["taskid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	return sql, param
}

/**
批量保存和更新
*/
func AddRelated(userid int64, username string, upd []TaskRelatedDto) error {
	o := orm.NewOrm()
	var err error
	to, err := o.Begin()
	// 如果无id 就新增
	utils.UUID.Init(1024)
	for _, v := range upd {
		related := new(ProjectTaskRelated)
		utils.SimpleCopyProperties(related, v)
		related.Created = time.Now().Unix()
		related.Creatid = userid
		// 如果有id 并且操作是1，删除当前数据
		if !utils.IsEmpty(v.Id) && v.Oper == 1 {
			_, err = to.Delete(related)
			if err != nil {
				to.Rollback()
				return err
			}
		} else if !utils.IsEmpty(v.Id) {
			// 如果有id 就更新数据
			_, err = to.Update(related)
			if err != nil {
				to.Rollback()
				return err
			}
		} else if utils.IsEmpty(v.Id) {
			stepid, _ := utils.UUID.GetId()
			related.Id = stepid
			_, err = to.Insert(related)
			if err != nil {
				to.Rollback()
				return err
			}
		}
	}
	if err != nil {
		to.Rollback()
		return err
	} else {
		to.Commit()
		return nil
	}
}
